﻿#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Web.Security;
#endregion

namespace DotNet.Common
{
    public class DotSession
    {
        /// <summary>
        /// 添加Session
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        static public void Add(string Key, string Value)
        {
            HttpContext.Current.Session[Key] = Value;
        }
        /// <summary>
        /// 存在性判断
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        static public bool Exists(string Key)
        {
            try
            {
                bool b = true;
                if (HttpContext.Current.Session[Key] == null || HttpContext.Current.Session[Key].ToString() == "")
                {
                    b = false;
                }
                return b;
            }
            catch (Exception ex)
            {
                throw new Exception(Key+" "+ex.Message);
            }
        }
        /// <summary>
        /// 删除Session
        /// </summary>
        /// <param name="Key"></param>
        static public void Delete(string Key)
        {
            HttpContext.Current.Session[Key] = "";
        }
        /// <summary>
        /// 读取Session
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        static public string Read(string Key)
        {
            if (Exists(Key))
            {
                return HttpContext.Current.Session[Key].ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 读取所有的Session
        /// </summary>
        /// <returns></returns>
        static public string ReadAll()
        {
            string ret="";
            for (int i = 0; i < HttpContext.Current.Session.Keys.Count; i++)
            {
                ret += "Key:"+HttpContext.Current.Session.Keys[i]+"|";
            }
            return ret;
        }
    }
}
